package cqrtplm.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.extra.spring.SpringUtil;
import com.alibaba.fastjson.JSONObject;
import com.hustcad.plm.pdm.ec.module.vo.ChgFormVO;
import com.hustcad.plm.pdm.ec.module.vo.ChgRequestVO;
import com.hustcad.plm.pdm.ec.module.vo.ECCommonVO;
import com.hustcad.plm.pdm.ec.service.TyplmChangeLinkService;
import com.hustcad.plm.pdm.ec.service.TyplmChgIssueRequestLinkService;
import com.hustcad.plm.pdm.ec.service.TyplmChgNoticeQueryService;
import com.hustcad.plm.pdm.ec.service.TyplmChgRequestNoticeLinkService;
import com.hustcad.plm.pdm.flowbusiness.model.entity.FlowChgFormView;
import com.hustcad.plm.pdm.iba.service.TyplmAttributeService;
import com.hustcad.plm.pdm.iba.service.TyplmAttributeValueService;
import com.hustcad.plm.pdm.lifestage.model.dto.EntityLifeCycleDTO;
import com.hustcad.plm.pdm.lifestage.service.TyplmCommonLifecycleService;
import com.hustcad.plm.pdm.partbom.service.TyplmPartBomService;
import com.hustcad.plm.pdm.partbom.service.TyplmPartService;
import com.hustcad.plm.pdm.partbom.service.TyplmPartUsageLinkService;
import com.hustcad.plm.pdm.partbom.service.view.TyplmViewDefService;
import com.hustcad.plm.pdm.workflow.model.entity.WorkFlowContainerView;
import com.hustcad.plm.pdm.workflow.service.TyplmWorkFlowContainerService;
import com.ty.basic.entity.change.ChgNoticeDO;
import com.ty.basic.handler.exceptionhandler.exception.ResponseCodeEnum;
import com.ty.basic.handler.exceptionhandler.exception.TyException;
import cqrtplm.constant.RTPlmConstant;
import cqrtplm.mapper.RTChangeMapper;
import cqrtplm.mapper.RtChangeaffectAssociatedbomMapper;
import cqrtplm.mapper.RtChangeaffectEbomMapper;
import cqrtplm.mapper.RtChangeaffectFinishedbomMapper;
import cqrtplm.service.SetRelatePRECRStatusService;
import cqrtplm.util.CommonUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigInteger;
import java.util.*;

@Service
@Slf4j
public class SetRelatePRECRStatusServiceImpl implements SetRelatePRECRStatusService {
    private static final int MAX_RETRY = 150; //设置最大重试次数防止死循环
    @Resource
    private TyplmWorkFlowContainerService typlmWorkFlowContainerService;
    @Resource
    private TyplmCommonLifecycleService typlmCommonLifecycleService;
    @Resource
    private TyplmChangeLinkService typlmChangeLinkService;
    @Resource
    private TyplmChgNoticeQueryService typlmChgNoticeQueryService;
    @Resource
    private TyplmChgIssueRequestLinkService typlmChgIssueRequestLinkService;
    public StringBuffer errMsg;

    /**
     * 修改PR ECR生命周期状态
     *
     * @param workFlowContainerId
     * @param PRStatus
     * @param ECStatus
     * @return
     */
    @Override
    public void SetRelatePRECRStatus(String workFlowContainerId, String PRStatus, String ECStatus) {
        WorkFlowContainerView workFlowContainerView = typlmWorkFlowContainerService.getWorkFlowContainerDetailByPrimaryKey(new BigInteger(workFlowContainerId));
        if (!(workFlowContainerView instanceof FlowChgFormView)) {
            return;
        }
        errMsg = new StringBuffer();
        FlowChgFormView flowChgFormView = (FlowChgFormView) workFlowContainerView;
        ChgFormVO chgFormVO = flowChgFormView.getChgFormVO();
        ChgNoticeDO chgNoticeDO = this.typlmChgNoticeQueryService.selectByPrimaryKey(chgFormVO.getOid());
        if (Objects.isNull(chgNoticeDO)) {
            TyException.throwSingleTyExceptionVarargs(ResponseCodeEnum.BIZ_OBJECT_NOT_FOUND, new String[]{chgFormVO.getOid().toString()});
        }
        //获取关联的ECR
        List<ChgRequestVO> requestVOS = typlmChangeLinkService.queryECRByECO(chgNoticeDO);
        List<EntityLifeCycleDTO> entityLifeCycleList = new ArrayList();
        if (CollUtil.isNotEmpty(requestVOS)){
            for (ChgRequestVO requestVO : requestVOS) {
                EntityLifeCycleDTO entityLifeCycleDTO = new EntityLifeCycleDTO();
                entityLifeCycleDTO.setOid(requestVO.getOid());
                entityLifeCycleDTO.setOtype(requestVO.getOtype());
                entityLifeCycleDTO.setLifeCycleStageKey(ECStatus);
                entityLifeCycleList.add(entityLifeCycleDTO);
                //获取关联的PR
                List<ECCommonVO> prList = this.typlmChgIssueRequestLinkService.getPrListByEcrList(Arrays.asList(requestVO.getOid()));
                if (CollUtil.isNotEmpty(prList)) {
                    for (ECCommonVO pr : prList){
                        entityLifeCycleDTO = new EntityLifeCycleDTO();
                        entityLifeCycleDTO.setOid(pr.getOid());
                        entityLifeCycleDTO.setOtype(pr.getOtype());
                        entityLifeCycleDTO.setLifeCycleStageKey(PRStatus);
                        entityLifeCycleList.add(entityLifeCycleDTO);
                    }
                }
            }
        }
//        CommonUtil.setProcessObjectLife("Y", targetLifeCycleStateKey, entityLifeCycleList);
        if (CollUtil.isEmpty(entityLifeCycleList)) {
            return;
        }
        typlmCommonLifecycleService.batchUpdateEntityLifecycle(entityLifeCycleList, true, true);
    }
}
